<!-- 表单 》 状态 -->

<template>
  <el-form-item v-bind="formItem">
    <!-- 标签 -->
    <template v-if="hasLabelSlot" #label>
      <slot name="label" :label="formItem.label">{{ formItem.label }}</slot>
    </template>

    <el-radio-group
      v-if="isBtn"
      v-model="model"
      v-bind="commonProps"
      :class="`${className} CoreWeb-RadioBtnGroup`"
      @change="emitChange"
    >
      <el-radio-button
        v-for="(item, i) in selectData"
        :key="`${item[0]}-${i}`"
        :class="getHideClass(item)"
        :label="item[0]"
        :disabled="getDisabled(item)"
      >
        <span :class="`CoreWeb-radio-btn CoreWeb-radio-btn-${item[colorIndex] || 'color'}`">
          <slot name="item" :item="item">
            <span>{{ item[1] }}</span>
            <span v-if="showRight" class="CoreWeb-radio-btn-right">{{ item[rightIndex] }}</span>
          </slot>
        </span>
      </el-radio-button>
    </el-radio-group>

    <el-radio-group
      v-else-if="isRadio"
      v-model="model"
      v-bind="commonProps"
      :class="`${className} CoreWeb-RadioGroup`"
      @change="emitChange"
    >
      <el-radio
        v-for="(item, i) in selectData"
        :key="`${item[0]}-${i}`"
        :class="getHideClass(item)"
        :label="item[0]"
        :disabled="getDisabled(item)"
      >
        <span :class="`CoreWeb-radio CoreWeb-radio-${item[colorIndex] || 'color'}`">
          <slot name="item" :item="item">
            <span>{{ item[1] }}</span>
            <span v-if="showRight" class="CoreWeb-radio-right">{{ item[rightIndex] }}</span>
          </slot>
        </span>
      </el-radio>
    </el-radio-group>

    <el-select
      v-else
      v-model="model"
      v-bind="commonProps"
      :filterable="!noSearch"
      :clearable="!noClear && !initFirst"
      @change="emitChange"
    >
      <el-option
        v-for="(item, i) in selectData"
        :key="`${item[0]}-${i}`"
        :class="getHideClass(item)"
        :value="item[0]"
        :label="item[1]"
        :disabled="getDisabled(item)"
      >
        <div :class="`CoreWeb-option CoreWeb-option-${item[colorIndex] || 'color'}`">
          <slot name="item" :item="item">
            <div>{{ item[1] }}</div>
            <div v-if="showRight" class="CoreWeb-option-right">{{ item[rightIndex] }}</div>
          </slot>
        </div>
      </el-option>
    </el-select>
  </el-form-item>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import mixForm from '../mixForm'
import FormSelect from '../FormSelect'

const name = 'FormStatus'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name,

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [mixForm],

  /**
   * 属性注册 (抽取以便查阅)
   */
  props: CORE.WEB[name],

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 是否显示右侧辅助信息
     */
    showRight: FormSelect.computed.showRight,

    /**
     * 弹出层样式类
     */
    popperClass: FormSelect.computed.popperClass,

    /**
     * 状态字典
     */
    STATUS() {
      const { dict, state, colorIndex } = this
      const STATUS = CORE.clone(dict || (state.DICT || {}).STATUS || CORE.DICT.STATUS)
      STATUS.forEach((item) => {
        const color = item[colorIndex]
        if (color && CORE.isString(color)) {
          const colorData = CORE.DICT.COLOR.find(([cn, en]) => color.includes(cn) || color.includes(en))
          if (colorData) item[colorIndex] = colorData[1]
        }
      })
      return STATUS
    },

    /**
     * 下拉框数据
     */
    selectData() {
      const { STATUS, functionType, functionIndex } = this
      if (functionType !== 'none') return STATUS
      return STATUS.filter((item) => !(CORE.isFunction(item[functionIndex]) && item[functionIndex]()))
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: FormSelect.methods,
}
</script>
